//
//  AdsProvider.m
//  AdsBox
//
//  Created by Andy on 13-10-15.
//  Copyright (c) 2013年 Andy. All rights reserved.
//

#import "AdsProvider.h"

#define KeyADPSte           @"KeyADPSte"
#define KeyADSingleton      @"KeyADSingleton"
#define KeyADUsed           @"KeyADUsed"

@implementation AdsProvider
static AdsProvider * shareProvider = nil;
+ (AdsProvider *)shareProvider{
    if (shareProvider == nil) {
        shareProvider = [[AdsProvider alloc] init];
    }
    return shareProvider;
}

- (NSInteger) providerCount{
    return [adsProviders count];
}

- (UIView *)requestAdsViewAtIndex:(int)index{
    NSMutableDictionary * ad = [self getAdDict:index];
    NSString * providerStr = [ad objectForKey:KeyADPSte];
    if (ad) {
        [ad setObject:[NSNumber numberWithBool:YES] forKey:KeyADUsed];
        if (ADP_MobWIN == providerStr) {
            return [self requestMobWINAdsView];
        } else if (ADP_BaiduAdMob == providerStr){
            return [self requestBaiduAdsView];
        } else if (ADP_GoogleAdMob == providerStr){
            return [self requestGoogleAdsView];
        }
    }
    return nil;
}

// private method
- (NSMutableDictionary *)getAdDict:(int)index{
    
    int indexInArray = index%[self providerCount];
    NSMutableDictionary * adDict = [adsProviders objectAtIndex:indexInArray];
    
    BOOL used = [[adDict objectForKey:KeyADUsed] boolValue];
    BOOL singleton = [[adDict objectForKey:KeyADSingleton] boolValue];
    if (used == NO || singleton == NO) {
        return adDict;
    }
    
    for (int i = 1; i<[self providerCount]; ++i) {
        int preIndex = indexInArray-i;
        if (preIndex>=0) {
            adDict = [adsProviders objectAtIndex:preIndex];
            used = [[adDict objectForKey:KeyADUsed] boolValue];
            singleton = [[adDict objectForKey:KeyADSingleton] boolValue];
            if (used == NO || singleton == NO) {
                return adDict;
            }
        }
        int nextIndex = indexInArray+i;
        if (nextIndex<[self providerCount]) {
            adDict = [adsProviders objectAtIndex:nextIndex];
            used = [[adDict objectForKey:KeyADUsed] boolValue];
            singleton = [[adDict objectForKey:KeyADSingleton] boolValue];
            if (used == NO || singleton == NO) {
                return adDict;
            }
        }
    }
    return nil;
}

- (id) init{
    adsProviders = [[NSMutableArray alloc] init];
    // add MobWin AD
    NSMutableDictionary * mobWinAd = [[NSMutableDictionary alloc] initWithDictionary:@{KeyADPSte: ADP_MobWIN, KeyADSingleton: [NSNumber numberWithBool:YES]}];
    [adsProviders addObject:mobWinAd];
    
    // add Baidu AD
    NSMutableDictionary * baiduAd = [[NSMutableDictionary alloc] initWithDictionary:@{KeyADPSte: ADP_BaiduAdMob, KeyADSingleton: [NSNumber numberWithBool:YES]}];
    [adsProviders addObject:baiduAd];
    
    // add google AD
    NSMutableDictionary * googleAd = [[NSMutableDictionary alloc] initWithDictionary:@{KeyADPSte: ADP_GoogleAdMob, KeyADSingleton: [NSNumber numberWithBool:YES]}];
    [adsProviders addObject:googleAd];
    
    
    return [super init];
}

- (UIView *)requestMobWINAdsView{
    GDTMobBannerView *bannerView = [[GDTMobBannerView alloc] initWithFrame:CGRectMake(0, 0,
                                                                                      GDTMOB_AD_SIZE_320x50.width,
                                                                                      GDTMOB_AD_SIZE_320x50.height)
                                                                    appkey:@"100720253"
                                                               placementId:@"9079537207574943610"];
    bannerView.delegate = self; // 设置Delegate
    bannerView.currentViewController = self.rootVC; //设置当前的ViewController
    bannerView.interval = 30; //【可选】设置刷新频率;默认30秒
    bannerView.isTestMode = YES; //【可选】设置测试模式;默认NO
    bannerView.isGpsOn = NO; //【可选】开启GPS定位;默认关闭
    //    [self.view addSubview:_bannerView]; //添加到当前的view中
    [bannerView loadAdAndShow]; //加载广告并展示
    return bannerView;
}

- (UIView *)requestBaiduAdsView{
    BaiduMobAdView * adView = [[BaiduMobAdView alloc] initWithFrame:CGRectMake(0.0,
                                                                               0.0,
                                                                               GAD_SIZE_320x50.width,
                                                                               GAD_SIZE_320x50.height)];
    adView.AdUnitTag = @"myAdPlaceId1";
    adView.AdType = BaiduMobAdViewTypeBanner;
    adView.delegate = self;
    [adView start];
    return adView;
    
}

- (UIView *)requestGoogleAdsView{
    GADBannerView *bannerView_ = [[GADBannerView alloc]
                                  initWithFrame:CGRectMake(0.0,
                                                           0.0,
                                                           GAD_SIZE_320x50.width,
                                                           GAD_SIZE_320x50.height)];
//    GADBannerView *bannerView_ = [[GADBannerView alloc] init];
    // 指定广告的“单元标识符”，也就是您的 AdMob 发布商 ID。
    bannerView_.adUnitID = GoogleAdMob_ID;
    
    // 告知运行时文件，在将用户转至广告的展示位置之后恢复哪个 UIViewController
    // 并将其添加至视图层级结构。
    bannerView_.rootViewController = self.rootVC;
    
    
    GADRequest *request = [GADRequest request];
    
    request.testDevices = [NSArray arrayWithObjects:
                           GAD_SIMULATOR_ID,                                           // 模拟器
                           nil];
    
    // 启动一般性请求并在其中加载广告。
    [bannerView_ loadRequest:request];
    
    return bannerView_;
}

#pragma mark - MobWIN delegate
// 请求广告条数据成功后调用
//
// 详解:当接收服务器返回的广告数据成功后调用该函数
- (void)bannerViewDidReceived{
    
}

// 请求广告条数据失败后调用
//
// 详解:当接收服务器返回的广告数据失败后调用该函数
- (void)bannerViewFailToReceived:(int)errCode{
    
}

// 全屏广告弹出时调用
//
// 详解:当广告栏被点击，弹出内嵌全屏广告时调用
- (void)bannerViewDidPresentScreen{
    
}

// 全屏广告关闭时调用
//
// 详解:当弹出内嵌全屏广告关闭，返回广告栏界面时调用
- (void)bannerViewDidDismissScreen{
    
}

// 应用进入后台时调用
//
// 详解:当点击下载或者地图类型广告时，会调用系统程序打开，
// 应用将被自动切换到后台
- (void)bannerViewWillLeaveApplication{
    
}
#pragma mark - BaiduAd Delegete
- (NSString *)publisherId
{
    return  @"debug"; //@"your_own_app_id";
}

- (NSString*) appSpec
{
    //注意：该计费名为测试用途，不会产生计费，请测试广告展示无误以后，替换为您的应用计费名，然后提交AppStore.
    return @"debug";
}


@end
